var app = getApp()
Page({
  data: {
    tempFilePaths: [],
    url: "",
    width: 0,
    height: 0,
    deblur: "",
    deblur_list: []
  },
  onLoad: function () {
  },
  chooseimage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log("res", res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          tempFilePaths: res.tempFilePaths
        })
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: function(res){
              _this.setData({
                width: res.width,
                height: res.height
              })
          }
        })
        wx.request({
          url: res.tempFilePaths[0],
          method: 'GET',
          responseType: 'arraybuffer',
          success: function (res) {
            var base64 = wx.arrayBufferToBase64(res.data);
            _this.setData({
              url: 'data:image/jpg;base64,' + base64
            })
            // console.log(_this.data.url);
          }
        });
      }
    })
  },

  detect() {
    wx.showLoading({
      title: '处理中，请稍后',
    })
    const _this = this;
    console.log("detect");
    wx.request({
      url: 'http://127.0.0.1:8888/api/image/deblur',
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      data: {
        base64: _this.data.url,
        width: _this.data.width,
        height: _this.data.height
      },
      success: res => {
        wx.hideLoading()
        const deblur = res.data.data;
        _this.setData({
          deblur: deblur,
          deblur_list: [deblur]
        })
      }
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接 
      urls: this.data.deblur_list // 需要预览的图片http链接列表 
    })
  },


})
